#pragma once

#include <iostream>
#include <string>
#include <sys/types.h>
#include <fstream>
#include <sys/stat.h>
#include <unistd.h>
#include <atomic>
#include <sys/time.h>

#include <boost/algorithm/string.hpp>

const std::string file_path = "./temp/";

namespace ns_util
{
    class TimeUtil
    {
    public:
        static std::string GetMicTime()
        {
            struct timeval tm;
            gettimeofday(&tm, nullptr);
            return std::to_string(tm.tv_sec * 1000 + tm.tv_usec / 1000);
        }
    };
    class PathUtil
    {
    public:
        static std::string AddSuffix(const std::string &file_name, const std::string &suffix)
        {
            std::string ret = file_path + file_name;
            ret += suffix;
            return ret;
        }

        // 编译时形成的临时文件
        static std::string Src(const std::string &file_name)
        {
            return AddSuffix(file_name, ".cpp");
        }
        static std::string Exe(const std::string &file_name)
        {
            return AddSuffix(file_name, ".exe");
        }
        static std::string CompileErr(const std::string &file_name)
        {
            return AddSuffix(file_name, ".compileerr");
        }

        // 运行时形成的临时文件
        static std::string Stdin(const std::string &file_name)
        {
            return AddSuffix(file_name, ".stdin");
        }
        static std::string Stdout(const std::string &file_name)
        {
            return AddSuffix(file_name, ".stdout");
        }
        static std::string Stderr(const std::string &file_name)
        {
            return AddSuffix(file_name, ".stderr");
        }
    };

    class FileUtil
    {
    public:
        // true 存在   false  不存在
        static bool FileIsExist(const std::string &file_path)
        {
            struct stat st;
            int n = ::stat(file_path.c_str(), &st);
            if (n < 0){
                return false;
            }
            else{
                return true;
            }
        }
        static std::string UniqueFileName()
        {
            static std::atomic_uint id(0);
            id++;
            std::string mic = TimeUtil::GetMicTime();
            std::string unique_id = std::to_string(id);
            
            return mic + "_" + unique_id;
        }
        static bool WriteToFile(const std::string &file_name, const std::string &content)
        {
            std::ofstream out(file_name);
            if (!out.is_open()){
                return false;
            }
            out.write(content.c_str(), content.size());
            out.close();
            
            return true;
        }
        static bool ReadFile(const std::string &file_name, std::string *content, bool keep = false)
        {
            (*content).clear();
            std::ifstream in(file_name);
            if (!in.is_open()){
                return false;
            }
            std::string line;
            while (std::getline(in, line)){
                (*content) += line;
                (*content) += (keep ? "\n" : "");
            }
            
            return true;
        }
    };

    class StringUtil
    {
    public:
        static bool Splitstring(const std::string &str, const std::string &sep, std::vector<std::string> *tokens)
        {
            boost::split( *tokens , str , boost::is_any_of(sep) , boost::algorithm::token_compress_on );

            return true;
        }
    };
}